package com.nulldev.util.sys.process;

import java.io.IOException;

import com.nulldev.util.java.ProcessUtil;
import com.nulldev.util.other.ArrayUtils;

public class Process {

	private long pid;
	private String name;
	public ProcessType type;
	private long memoryUsage;
	private String args;
	private String title = "N/A";

	public static enum ProcessType {
		Process, Service, Console, UNKNOWN;
	}

	public Process(final long pid, final String name, final ProcessType type) {
		this.pid = pid;
		this.name = name;
		this.type = type;
	}
	
	public boolean isBlank() {
		return this == ProcessManager.BLANK;
	}

	public long pid() {
		return this.pid;
	}

	public String getExecutable() {
		return this.name;
	}

	public ProcessType getType() {
		return this.type;
	}
	
	public String getArgs() {
		if (this.args == null) {
			return "";
		}
		return this.args;
	}

	public String execute(final String[] args) throws IOException {
		return ProcessUtil.execCmd(this.name + ArrayUtils.StringArrayUtils.addFrom(args, 0));
	}
	
	public String getTitle() {
		return this.title;
	}
	
	public void setTitle(final String title) {
		this.title = title;
	}

	public void setMemoryUsage(final long K) {
		this.memoryUsage = K;
	}

	public long memoryUsage() {
		return this.memoryUsage;
	}

	@Override
	public String toString() {
		return "Process '" + this.name + "' [" + this.title + "] has a PID of '" + this.pid + "' and is a '" + this.type + "' based process and was using '" + this.memoryUsage + "' K of RAM.";
	}

	public void setArgs(final String args) {
		this.args = args;
	}
}
